//var treeDate = {
//			// "hLine":5000,//有目标线
//			"hLine":0,//无目标线
//			"nullTips":'暂无数据',//是否有数据
//			"xLables":['一','二','三','四','五','六','七'],//底部文字
//			"yValues":[2000,1500,8000,9000,10000,7000,5000]//树状图的长度
//			// "yValues":[]//无数据
//		};
//测试
function testDrawTree(){
//	var canvas = document.getElementById("mod-sleep-canvas");
//	var treeTW = document.getElementById("mod-sleep-rTree").offsetWidth;
//	drawTreeToHome(treeTW * 2,75 * 2,canvas,"#f7882d","#f7882d","gray",treeDate);
}




/**
 *绘制树状图 微动_微廋首页
 */
function drawTreeToHome(width,height,canvas,vLineColor,hLineColor,textColor,treeDate){
	//width 160px height 75
	canvas.width = width;
	canvas.height = height;
	var ctx = canvas.getContext("2d");
	// console.log("width:" + width + "---height:" + height);
	var xTexts = treeDate.xLables;
	var yValues = treeDate.yValues;
	//第一条线位置	
	var lineXone = width * 0.025;	
	//底部文字位置
	var xTextIndex = 7.5;
	var hLineMaxHeight = 50;
	var hLineY = 20;
	var vLineWidth = 6;
	var hLineWidth = 2;

	//设置高度比例
	xTextIndex = height * xTextIndex / 75;
	hLineMaxHeight = height * hLineMaxHeight / 75;
	hLineY = height * hLineY / 75;

	//底部文字
	ctx.beginPath();
	ctx.fillStyle = textColor;
	ctx.font = '26px Helvetica';
	ctx.textBaseline = "middle";
	ctx.textAlign = 'center';

	var textW = width * 0.95 / xTexts.length;

	for(var i = 0; i < xTexts.length; i++){
		ctx.fillText(xTexts[i], lineXone + textW * i + ctx.measureText(xTexts[i]).width ,height - xTextIndex);
	}
	ctx.closePath();
	
	if(yValues.length > 0){
		//树状图最大值
		var yValuesMax = Math.max.apply(null,yValues);
		yValuesMax = yValuesMax > treeDate.hLine ? yValuesMax : treeDate.hLine;
		//填充树状图
		ctx.beginPath();
		ctx.strokeStyle = vLineColor;
		ctx.lineWidth = vLineWidth;
		
		for(var i = 0; i < yValues.length; i++){
			ctx.moveTo(lineXone + textW * i + ctx.measureText(xTexts[i]).width,height - hLineY);
			ctx.lineTo(lineXone + textW * i + ctx.measureText(xTexts[i]).width,height - hLineY - yValues[i] / yValuesMax * hLineMaxHeight);
		}
		ctx.closePath();
		ctx.stroke();
		
		//绘制目标线
		if(treeDate.hLine > 0){
			drawHDashed(ctx,hLineColor,hLineWidth,width * 0.025,width * 0.975, height - hLineY - treeDate.hLine / yValuesMax * hLineMaxHeight,30);
		}
	}else{
		//没数据的时候
		if(treeDate.hLine > 0){

			ctx.beginPath();
			ctx.fillStyle = textColor;
			ctx.font = "30px Helvetica";
			ctx.textBaseline = "bottom";
			ctx.textAlign = "center";
			ctx.fillText(treeDate.nullTips,width / 2,height / 1.5);
			ctx.closePath();
			//绘制目标线
			drawHDashed(ctx,hLineColor,hLineWidth,width * 0.025,width * 0.975, height / 3,30);

		}else{
			ctx.beginPath();
			ctx.fillStyle = textColor;
			ctx.font = "30px Helvetica";
			ctx.textBaseline = "bottom";
			ctx.textAlign = "center";
			ctx.fillText(treeDate.nullTips,width / 2,height - hLineY);
			ctx.closePath();	
		}
		
	}

	

}
//绘制横线虚线
function drawHDashed(ctx,lineColor,lineWidth,xStart,xEnd,yIndex,lienCount){
	ctx.beginPath();
	ctx.strokeStyle = lineColor;
	ctx.lineWidth = lineWidth;			
	//横线总长度
	var hLineLength = xEnd - xStart;
	for(var i = 0; i < lienCount; i++ ){
		if(i % 2 == 0){
			ctx.moveTo(xStart + i * hLineLength / lienCount,yIndex);
			ctx.lineTo(xStart + (i + 1) * hLineLength / lienCount,yIndex);	
		}
	}
	ctx.closePath();
	ctx.stroke();
}
/**
 * 绘制首页睡眠图表
 * 由于数据格式改变，所以采用单独的方法绘制首页睡眠的树状图
 * @param width
 * @param height
 * @param canvas
 * @param vLineColor
 * @param hLineColor
 * @param textColor
 * @param treeDate
 * @author wangguang
 */
function drawTreeToHomeSleep(width,height,canvas,vLineColor,hLineColor,textColor,treeDate){
	//width 160px height 75
	canvas.width = width;
	canvas.height = height;
	var ctx = canvas.getContext("2d");
	// console.log("width:" + width + "---height:" + height);
	var xTexts = treeDate.xLables.split(",");
	var yValues = treeDate.yValues.split(",");
	
	//第一条线位置	
	var lineXone = width * 0.025;	
	//底部文字位置
	var xTextIndex = 7.5;
	var hLineMaxHeight = 50;
	var hLineY = 20;
	var vLineWidth = 6;
	var hLineWidth = 2;

	//设置高度比例
	xTextIndex = height * xTextIndex / 75;
	hLineMaxHeight = height * hLineMaxHeight / 75;
	hLineY = height * hLineY / 75;

	//底部文字
	ctx.beginPath();
	ctx.fillStyle = textColor;
	ctx.font = '26px Helvetica';
	ctx.textBaseline = "middle";
	ctx.textAlign = 'center';

	var textW = width * 0.95 / xTexts.length;

	for(var i = 0; i < xTexts.length; i++){
		ctx.fillText(xTexts[i], lineXone + textW * i + ctx.measureText(xTexts[i]).width ,height - xTextIndex);
	}
	ctx.closePath();
	
	if(treeDate.weekSleep > 0){
		//树状图最大值
		var yValuesMax = Math.max.apply(null,yValues);
		yValuesMax = yValuesMax > treeDate.hLine ? yValuesMax : treeDate.hLine;
		//填充树状图
		ctx.beginPath();
		ctx.strokeStyle = vLineColor;
		ctx.lineWidth = vLineWidth;
		
		for(var i = 0; i < yValues.length; i++){
			ctx.moveTo(lineXone + textW * i + ctx.measureText(xTexts[i]).width,height - hLineY);
			ctx.lineTo(lineXone + textW * i + ctx.measureText(xTexts[i]).width,height - hLineY - yValues[i] / yValuesMax * hLineMaxHeight);
		}
		ctx.closePath();
		ctx.stroke();
		
		//绘制目标线
		if(treeDate.hLine > 0){
			drawHDashed(ctx,hLineColor,hLineWidth,width * 0.025,width * 0.975, height - hLineY - treeDate.hLine / yValuesMax * hLineMaxHeight,30);
		}
	}else{
		//没数据的时候
		if(treeDate.hLine > 0){

			ctx.beginPath();
			ctx.fillStyle = textColor;
			ctx.font = "30px Helvetica";
			ctx.textBaseline = "bottom";
			ctx.textAlign = "center";
			ctx.fillText(treeDate.nullTips,width / 2,height / 1.5);
			ctx.closePath();
			//绘制目标线
			drawHDashed(ctx,hLineColor,hLineWidth,width * 0.025,width * 0.975, height / 3,30);

		}else{
			ctx.beginPath();
			ctx.fillStyle = textColor;
			ctx.font = "30px Helvetica";
			ctx.textBaseline = "bottom";
			ctx.textAlign = "center";
			ctx.fillText(treeDate.nullTips,width / 2,height - hLineY);
			ctx.closePath();	
		}
		
	}

	

}
